<?php
/**
 *Administra las promociones del sitio web
 *@author Luis Gabriel Rodriguez <luisgabriel84@gmail.com>
 */

/**
 *Administra las promociones del sitio web
 */
class PromotionsController extends AppController {

	var $name = 'Promotions';
        /**
        * @var array
        * @access public
        */
	var $helpers = array('Html', 'Form', 'Javascript', 'Time', 'Flash');
        /**
        * @var array
        * @access public
        */
        var $components = array('Filehandler');

        /**
         * Listado de promociones para el usuario
         */
	function index() {
		$this->Promotion->recursive = 0;
		$this->set('promotions', $this->paginate());
	}

	 /**
         * Listado de promociones para el administrador
         */
	function admin_index() 
	{
		$this->layout = 'admin';
		$this->Promotion->recursive = 0;
		$this->set('promotions', $this->paginate());
	}
        /**
         *
         * Muestra la promocion
         * @param integer $id Identificador de la promocion
         */
	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Promotion.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('promotion', $this->Promotion->read(null, $id));
	}

        /**
         *
         * Agrega una  promocion
         */
	function admin_add() 
	{
		$this->layout = 'admin';
		if (!empty($this->data))
                {

                    $this->Promotion->create();
                    if(!empty( $this->data['Promotion']['file']['name'] ))
                    {
                        $foto = $this->data['Promotion']['file']  = $this->Filehandler->uploadpicture('img/promotions/', $this->data['Promotion']['file'] ,200,100);
                    }
                    else
                    {
                        $this->data['Promotion']['file'] ='no-photo.jpg';
                    }
                    if ($this->Promotion->save($this->data)) {
                            $this->Session->setFlash(__('La promocion se guardo', true));
                            $this->redirect(array('action'=>'index'));
                    } else {
                            $this->Session->setFlash(__('The Promotion could not be saved. Please, try again.', true));
                    }
		}

		$products = $this->Promotion->Product->find('list',array('fields'=>array('nombre_empresa')));
		$this->set(compact('products'));
	}

        /**
         *
         * Modifica una promocion
         * @param integer $id Identificador de la promocion
         */
	function admin_edit($id = null) 
	{
                $this->layout = 'admin';
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Promotion', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
		
		//Inicia actualizacicion de foto			
                        $this->data_update = $this->Promotion->read(null, $id);
			if( !empty($this->data['Promotion']['file']['name']))
			{
			    $foto = $this->data_update['Promotion']['file']; 
                            $this->Filehandler->deleteFile('img/promotions/',$foto,true);
			    $this->data['Promotion']['file']  = $this->Filehandler->uploadpicture('img/promotions/', $this->data['Promotion']['file'] ,200,100);
			}
			else
			{
			    $this->data['Promotion']['file'] = $this->data_update['Promotion']['file']; 
			}
                        //FIN actualizacicion de foto
			if ($this->Promotion->save($this->data)) {
				$this->Session->setFlash(__('La promocion fue guardada', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Promotion could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Promotion->read(null, $id);
		}
		$products = $this->Promotion->Product->find('list',array('fields'=>'nombre_empresa'));
		$this->set(compact('products'));
	}

        /**
         *
         * Elimina una promocion
         * @param integer id Identificador de la promocion
         */
	function admin_delete($id = null) {
	$this->layout = 'admin';
        if (!$id)
        {
                $this->Session->setFlash(__('Invalid id for Promotion', true));
                $this->redirect(array('action'=>'index'));
        }
		
		//inicia eliminar archivo
            $this->data_update = $this->Promotion->read(null, $id);
	    $foto = $this->data_update['Promotion']['file']; 
			    
            $this->Filehandler->deleteFile('img/promotions/',$foto,true);
            //fin eliminar archivo
		if ($this->Promotion->del($id)) {
			$this->Session->setFlash(__('Promotion Eliminado', true));
			$this->redirect(array('action'=>'index'));
		}
	}
}
?>